package com.xc.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.demo.entity.Customer;
import com.xc.demo.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerController {


    @Autowired
    private CustomerService customerService;


    //    新增数据
    @PostMapping
    public boolean save(@RequestBody Customer customer){
        return customerService.saveOrUpdate(customer);
    }


    //    更新用户数据
    @PutMapping
    public Customer update(@RequestBody Customer customer){

        customerService.updateById(customer);

        return customer;


    }


    //实现分页效果
    @GetMapping("/page")
    public IPage<Customer> findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam(defaultValue = "") String uname

    ){
        log.info("pageNum={},pageSize={},username={},accountName={}",pageNum,pageSize,uname);

        IPage<Customer> page= new Page<>(pageNum,pageSize);

        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();

        if(!"".equals(uname)){
            queryWrapper.like("nickname",uname);
        }



        queryWrapper.orderByAsc("id");


        return customerService.page(page,queryWrapper);
    }

}
